package com.generator.mybatis.generator.core.ganerator;

import com.generator.mybatis.generator.core.config.EntityConfig;
import com.generator.mybatis.generator.core.enums.DatabaseTypeEnum;
import com.generator.mybatis.generator.core.enums.OpenEnum;
import com.generator.mybatis.generator.core.enums.TemplateEnum;
import com.generator.mybatis.generator.util.CreateTemplateUtil;
import com.generator.mybatis.generator.util.WordUtil;
import com.generator.mybatis.generator.core.vo.ColumnVO;
import com.generator.mybatis.generator.core.vo.TableVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CreateEntity {
    private String path;
    private EntityConfig entityConfig;
    private OpenEnum swaggerOpen;
    public CreateEntity(String path, EntityConfig entityConfig,OpenEnum swaggerOpen){
        this.path=path;
        this.entityConfig=entityConfig;
        this.swaggerOpen=swaggerOpen;
    }
    public void createFile(List<TableVO> tables){
        if(entityConfig.getOpen().equals(OpenEnum.OFF)){
            return;
        }
        for(TableVO table:tables){
            try {
                String className= WordUtil.getClassName(table.getName())+entityConfig.getSuffix();
                String classPath=path+"/"+entityConfig.getPackagePath().replaceAll("\\.","/")+"/"+className+".java";
                List<String> importPackages=new ArrayList<>();
                DatabaseTypeEnum databaseTypeEnum;
                for(ColumnVO column:table.getColumns()){
                    //字段名全部转小写
                    column.setName(column.getName().toLowerCase());
                    //数据库类型转java类型
                    try{
                        databaseTypeEnum=DatabaseTypeEnum.valueOf(column.getType().toUpperCase());
                    }catch(Exception e){
                        databaseTypeEnum=DatabaseTypeEnum.VARCHAR;
                    }
                    column.setType(databaseTypeEnum.getJavaType());
                    //导入类型所用的的包
                    if(!databaseTypeEnum.getImportPackage().equals("")&&!importPackages.contains(databaseTypeEnum.getImportPackage())){
                        importPackages.add(databaseTypeEnum.getImportPackage());
                    }
                    //下划线命名转小驼峰
                    column.setSamllHumpName(WordUtil.getLowerHump(column.getName()));
                    column.setBigHumpName(WordUtil.getUpperHump(column.getName()));
                }
                Map<String,Object> params=new HashMap<>();
                params.put("comment",table.getComment());
                params.put("columns",table.getColumns());
                params.put("tableName",table.getName());
                params.put("className",className);
                params.put("entityPath",entityConfig.getPackagePath());
                params.put("importPackages",importPackages);
                params.put("swaggerOpen",swaggerOpen.equals(OpenEnum.ON)?true:false);
                CreateTemplateUtil.createFile(TemplateEnum.entity.toString(),classPath, params);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
